#pragma once
#include <iostream>
#include <unordered_map>
#include "Protocol.hpp"
#include "../Connection.hpp"
#include "Request&&Response.hpp"
#include "../RoomPlayers.hpp"
#include "../Time.hpp"

// 玩家准备时的请求以及响应格式
// 请求报文中会表明这是一个玩家准备的报文
// Request: CreateRoom -> $len${ "server_type" : "QuitRoom"}
// 响应会处理请求报文，本地直接退出即可

extern std::unordered_map<std::string, Room *> rooms;
extern std::unordered_map<int, std::string> fd_to_room_number;

class RequestPlayGameRoom : public Request
{
public:
    RequestPlayGameRoom() {}

    virtual bool Serialize(std::string *msg)
    {
        // Json::Value root;
        // root["server_type"] = PlayGame;
        // //root["room_number"] = _room_number;
        // Json::FastWriter writer;
        // *msg = writer.write(root);

        return true;
    }
    virtual bool Deserialize(const Json::Value &root)
    {
        return true;
    }

    ~RequestPlayGameRoom() {}

private:
};

class ResponsePlayGameRoom : public Response
{
public:
    ResponsePlayGameRoom() {}

    virtual bool Serialize(std::string *msg)
    {
        return true;
    }

    bool ServerHandler(const RequestPlayGameRoom *req, Connection *conn)
    {
        std::vector<Player> &players = rooms[fd_to_room_number[conn->GetFd()]]->GetPlayers();

        bool flag1 = true, flag2 = true;
        if (!(rooms[fd_to_room_number[conn->GetFd()]]->GetMaxMember() == rooms[fd_to_room_number[conn->GetFd()]]->GetPlayersNumber().size()))
            flag1 = false;

        if (flag1)
        {
            for (int i = 0; i < players.size(); i++)
            {
                Connection *conn_player = players[i].GetConnection();
                if (conn != conn_player)
                {
                    if(!players[i].Ready())
                    {
                        flag2 = false;
                        break;
                    }
                }
            }
        }

        // std::cout << "play game: " << flag1 << " " << flag2 << std::endl;
        if (flag1 && flag2)
        {
            std::string msg;
            Json::Value root;
            root["server_type"] = PlayGame;
            Json::FastWriter writer;
            msg = writer.write(root);
            // std::cout << "play game room msg: " << msg;
            Package(msg, &msg);
            for (int i = 0; i < players.size(); i++)
            {
                if (players[i].Exist())
                {
                    Connection *conn_player = players[i].GetConnection();
                        conn_player->GetOutbuffer() += msg;
                        // std::cout << "conn_player->GetOutbuffer() " << conn_player->GetOutbuffer() << std::endl;
                        conn_player->GetWriter()(conn_player);
                }
            }

            rooms[fd_to_room_number[conn->GetFd()]]->SetStartDownCount(Time::GetCurTimeStamp());
        }

        return true;
    }

    virtual bool Deserialize(const std::string &result)
    {

        return true;
    }

    virtual ~ResponsePlayGameRoom() {}

private:
};
